#include<conio.h>
#include<stdio.h>
#include<fstream>
#include<string.h>
#define MAX 200

void NhapDuLieu(int &m, int &n,int A[MAX][MAX]);
int TinhTriSo(int i, int j, int A[MAX][MAX], int m, int n);
// Dong Van Phu da chinh sua tren nay, dung SVN de Commit
void main()
{
	int A[MAX][MAX], m,n;
	int B[MAX][MAX];// ma tran tri so o cau c
	NhapDuLieu(m,n,A);
	//in du lieu ra de test coi nhap dung chua
	printf("m = %d, n = %d\n",m,n);
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
		{
			printf("%2d\t",A[i][j]);


		}
		printf("\n");
	}

	//Dem so min 
	int somin=0;
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
		{			
			if(A[i][j]==-1)
				somin++;			
		}		
	}

	//=== in so min ra
	printf("%d\n",somin);

	for(int i=0;i<m;i++)
		for(int j=0;j<n;j++)
		{
			if(A[i][j]==0)
				B[i][j]=TinhTriSo(i,j,A,m,n);
			else
				B[i][j]=9;
		}

		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(B[i][j]!= 9)
					printf("%2d\t",B[i][j]);
				else
					printf("  \t");		

			}
			printf("\n");
		}

		int a=TinhTriSo(5,4,A,m,n);
		printf("%d",a);
		_getch();
}
void NhapDuLieu(int &m, int &n,int A[MAX][MAX])
{
	FILE *fi;
	fi=fopen("INPUT.txt","rt");
	fscanf(fi,"%d",&m);
	fscanf(fi,"%d",&n);
	for (int i=0;i<m;i++)
		for (int j=0;j<n;j++)
		{
			fscanf(fi,"%d",&A[i][j]);
		}
		fclose(fi);
}
int TinhTriSo(int i, int j, int A[MAX][MAX], int m, int n){
	int triso=0;
	if(A[i][j]==0){

		if(A[i-1][j-1]==-1)
			triso++;
		if(A[i-1][j]==-1)
			triso++;
		if(A[i-1][j+1]==-1)
			triso++;
		if(A[i][j-1]==-1)
			triso++;	
		if(A[i][j+1]==-1)
			triso++;

		if(A[i+1][j-1]==-1)
			triso++;
		if(A[i+1][j]==-1)
			triso++;
		if(A[i+1][j+1]==-1)
			triso++;
	}
	else
		triso=9;

	return triso;
}